

#ifndef _EVENTLOOP_THREADPOOL_H_
#define _EVENTLOOP_THREADPOOL_H_

#include "noncopyable.hpp"
#include <functional>
#include <memory>
#include <string>
#include <vector>

namespace zephyr {

class EventLoop;
class EventLoopThread;

class EventLoopThreadPool : noncopyable {
  public:
    using ThreadInitCallback = std::function<void(EventLoop *)>;
    EventLoopThreadPool(EventLoop *base_loop, const std::string &name);
    ~EventLoopThreadPool();

    void setThreadNum(int numThreads) { m_numThread = numThreads; }
    void start(const ThreadInitCallback &cb = ThreadInitCallback());

    EventLoop *getNextLoop();

    std::vector<EventLoop *> getAllLoop();
    bool started() { return m_started; }

    const std::string name() const { return m_name; }

  private:
    EventLoop *base_loop; // 用户创建的loop
    std::string m_name;
    bool m_started;
    int m_numThread;
    int m_next; // 新连接到来 选择eventloop的索引
    std::vector<std::unique_ptr<EventLoopThread>> m_threads;
    std::vector<EventLoop *> m_loops;
};

}

#endif // _EVENTLOOP_THREADPOOL_H_